package com.ehualu.check.utils;

import com.ehualu.check.db.DBCloseUtills;
import com.ehualu.check.db.GetDBConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author: 吴敬超
 * @Date: 2020/7/13 11:59
 */
public class MysqlUtil {

    private static final Logger LOG = LoggerFactory.getLogger(MysqlUtil.class);


    /**
     * 获取 ID，文件位置，校验模式
     *
     * @return 返回查询集合, 每条数据包括 ID，文件位置，和校验模式
     */
    public List<Map<String, String>> getCheckinfo() {


        GetDBConnection dbConnection = new GetDBConnection();
        Connection conn = dbConnection.getdbconn();


        List<Map<String, String>> maplist = new ArrayList<Map<String, String>>();

        Statement st = null;
        ResultSet rs = null;
        try {
            st = conn.createStatement();


//            获取 ID，文件位置，校验模式
            String sqlgetcheck = "SELECT id,ext_path,ext_hash_mothod FROM dcp.da_ext where ext_hash_status = '0' and status = 'COMPLETED' ";


            LOG.info("sqlgetcheck is " + sqlgetcheck);


            rs = st.executeQuery(sqlgetcheck);

            while (rs.next()) {


                String id = rs.getString("id");
                String extPath = rs.getString("ext_path");
                String extHashMothod = rs.getString("ext_hash_mothod");
                Map<String, String> map = new HashMap<String, String>();


                map.put("id", id);
                map.put("ext_path", extPath);
//                map.put("ext_hash_mothod", extHashMothod);

                maplist.add(map);


            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            DBCloseUtills dbCloseUtills = new DBCloseUtills();
            dbCloseUtills.release(conn, st, rs);


        }

        return maplist;


    }

    /**
     * 只有一种校验码写入数据库
     * @param id
     * @param extHash
     * @param extHashStatus
     */
    public void updateCheckinfo(String id, String extHash, String extHashStatus) {


        GetDBConnection dbConnection = new GetDBConnection();
        Connection conn = dbConnection.getdbconn();


        Statement st = null;
        ResultSet rs = null;
        try {
            st = conn.createStatement();

            String sqlupdate = String.format("UPDATE dcp.da_ext set ext_hash_status = '%s',ext_hash='%s'  WHERE id = '%s'", extHashStatus, extHash, id);


            LOG.info("修改sql sqlupdate is " + sqlupdate);


            st.executeUpdate(sqlupdate);


        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            DBCloseUtills dbCloseUtills = new DBCloseUtills();
            dbCloseUtills.release(conn, st, rs);


        }


    }


    /**
     * 两种校验码均写入数据库
     * @param id
     * @param MD5Hash
     * @param sha256Hash
     * @param extHashStatus
     */
    public void updateCheckinfo(String id, String MD5Hash, String sha256Hash, String extHashStatus) {


        GetDBConnection dbConnection = new GetDBConnection();
        Connection conn = dbConnection.getdbconn();


        Statement st = null;
        ResultSet rs = null;
        try {
            st = conn.createStatement();

            String sqlupdate = String.format("UPDATE dcp.da_ext set ext_hash_status = '%s',md5_hash='%s',sha256_hash='%s'  WHERE id = '%s'", extHashStatus, MD5Hash, sha256Hash, id);


            LOG.info("修改sql sqlupdate is " + sqlupdate);


            st.executeUpdate(sqlupdate);


        } catch (Exception e) {
            LOG.info("MySQL写入校验码失败");
            e.printStackTrace();
        } finally {

            DBCloseUtills dbCloseUtills = new DBCloseUtills();
            dbCloseUtills.release(conn, st, rs);


        }


    }


/*
    public static void main(String[] args) {


        MysqlUtil mysqlUtil = new MysqlUtil();
        List<Map<String, String>> checkInfoList = mysqlUtil.getCheckinfo();
        for (int i = 0; i < checkInfoList.size(); i++) {


            System.out.println(checkInfoList.get(i).get("id"));
            System.out.println(checkInfoList.get(i).get("ext_path"));
            String mo=checkInfoList.get(i).get("ext_hash_mothod");


        }
    }
*/

}
